package org.yum.gen;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class Panel extends JPanel {
	private JTextField powerName;
	private JTextField powerType;
	private JLabel lblKeywords;
	private JTextField powerKeywords;
	private JLabel lblActionType;
	private JTextField powerActionType;
	private JLabel lblRange;
	private JTextField powerRange;
	private JLabel lblTarget;
	private JTextField powerTarget;
	private JLabel lblAttack;
	private JTextField powerAttack;
	private JLabel lblHit;
	private JTextField powerHit;
	private JLabel lblEffect;
	private JTextArea powerEffect;
	private JCheckBox chckbxNoEffect;
	private JButton btnGenerate;
	private JLabel lblWaiting;
	private JButton btnSave;


	// Create instance of the CardView class.
	private CardView card;
	/**
	 * Create the panel.
	 */
	public Panel(Generator gen, CardView c) {
		
		card = c; // Synchronize the card instance.
		
		setBackground(new Color(255, 222, 173));
		
		
		
		// The majority of this class is just Swing stuff.
		
		// Here we set our layout to GridBag. This results in a bunch of rows and columns
		// to place things in.
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] {120, 200, 0};
		gridBagLayout.rowHeights = new int[] {38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 0};
		
		setLayout(gridBagLayout);
		
		/*
		 * Begin creation of components. Nothing real exciting here.
		 */
		
		JLabel lblName = new JLabel("Name");
		lblName.setHorizontalAlignment(SwingConstants.RIGHT);
		lblName.setAlignmentY(Component.TOP_ALIGNMENT);
		GridBagConstraints gbc_lblName = new GridBagConstraints();
		gbc_lblName.fill = GridBagConstraints.BOTH;
		gbc_lblName.insets = new Insets(0, 0, 5, 5);
		gbc_lblName.gridx = 0;
		gbc_lblName.gridy = 0;
		add(lblName, gbc_lblName);
		
		powerName = new JTextField();
		powerName.setBackground(new Color(255, 222, 173));
		powerName.setText("Example");
		powerName.setAlignmentY(Component.TOP_ALIGNMENT);
		powerName.setAlignmentX(Component.LEFT_ALIGNMENT);
		GridBagConstraints gbc_txtName = new GridBagConstraints();
		gbc_txtName.fill = GridBagConstraints.BOTH;
		gbc_txtName.insets = new Insets(0, 0, 5, 0);
		gbc_txtName.gridx = 1;
		gbc_txtName.gridy = 0;
		add(powerName, gbc_txtName);
		powerName.setColumns(10);
		
		JLabel lblType = new JLabel("Type");
		lblType.setHorizontalAlignment(SwingConstants.RIGHT);
		GridBagConstraints gbc_lblType = new GridBagConstraints();
		gbc_lblType.fill = GridBagConstraints.BOTH;
		gbc_lblType.insets = new Insets(0, 0, 5, 5);
		gbc_lblType.gridx = 0;
		gbc_lblType.gridy = 1;
		add(lblType, gbc_lblType);
		
		powerType = new JTextField();
		powerType.setBackground(new Color(255, 222, 173));
		powerType.setText("At will");
		GridBagConstraints gbc_txtAtWillEtc = new GridBagConstraints();
		gbc_txtAtWillEtc.fill = GridBagConstraints.BOTH;
		gbc_txtAtWillEtc.insets = new Insets(0, 0, 5, 0);
		gbc_txtAtWillEtc.gridx = 1;
		gbc_txtAtWillEtc.gridy = 1;
		add(powerType, gbc_txtAtWillEtc);
		powerType.setColumns(10);
		
		lblKeywords = new JLabel("Attributes");
		lblKeywords.setHorizontalAlignment(SwingConstants.RIGHT);
		GridBagConstraints gbc_lblAttributes = new GridBagConstraints();
		gbc_lblAttributes.fill = GridBagConstraints.BOTH;
		gbc_lblAttributes.insets = new Insets(0, 0, 5, 5);
		gbc_lblAttributes.gridx = 0;
		gbc_lblAttributes.gridy = 2;
		add(lblKeywords, gbc_lblAttributes);
		
		powerKeywords = new JTextField();
		powerKeywords.setBackground(new Color(255, 222, 173));
		powerKeywords.setText("Radiant, Weapon");
		GridBagConstraints gbc_txtRadiantEtc = new GridBagConstraints();
		gbc_txtRadiantEtc.fill = GridBagConstraints.BOTH;
		gbc_txtRadiantEtc.insets = new Insets(0, 0, 5, 0);
		gbc_txtRadiantEtc.gridx = 1;
		gbc_txtRadiantEtc.gridy = 2;
		add(powerKeywords, gbc_txtRadiantEtc);
		powerKeywords.setColumns(10);
		
		lblActionType = new JLabel("Action Type");
		lblActionType.setHorizontalAlignment(SwingConstants.RIGHT);
		GridBagConstraints gbc_lblActionType = new GridBagConstraints();
		gbc_lblActionType.fill = GridBagConstraints.BOTH;
		gbc_lblActionType.insets = new Insets(0, 0, 5, 5);
		gbc_lblActionType.gridx = 0;
		gbc_lblActionType.gridy = 3;
		add(lblActionType, gbc_lblActionType);
		
		powerActionType = new JTextField();
		powerActionType.setBackground(new Color(255, 222, 173));
		powerActionType.setText("Standard Action");
		GridBagConstraints gbc_txtFree = new GridBagConstraints();
		gbc_txtFree.fill = GridBagConstraints.BOTH;
		gbc_txtFree.insets = new Insets(0, 0, 5, 0);
		gbc_txtFree.gridx = 1;
		gbc_txtFree.gridy = 3;
		add(powerActionType, gbc_txtFree);
		powerActionType.setColumns(10);
		
		lblRange = new JLabel("Range");
		lblRange.setHorizontalAlignment(SwingConstants.RIGHT);
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.fill = GridBagConstraints.BOTH;
		gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel.gridx = 0;
		gbc_lblNewLabel.gridy = 4;
		add(lblRange, gbc_lblNewLabel);
		
		powerRange = new JTextField();
		powerRange.setBackground(new Color(255, 222, 173));
		powerRange.setText("Melee weapon");
		GridBagConstraints gbc_txtMeleeWeapon = new GridBagConstraints();
		gbc_txtMeleeWeapon.fill = GridBagConstraints.BOTH;
		gbc_txtMeleeWeapon.insets = new Insets(0, 0, 5, 0);
		gbc_txtMeleeWeapon.gridx = 1;
		gbc_txtMeleeWeapon.gridy = 4;
		add(powerRange, gbc_txtMeleeWeapon);
		powerRange.setColumns(10);
		
		lblTarget = new JLabel("Target");
		lblTarget.setHorizontalAlignment(SwingConstants.RIGHT);
		GridBagConstraints gbc_lblTarget = new GridBagConstraints();
		gbc_lblTarget.fill = GridBagConstraints.BOTH;
		gbc_lblTarget.insets = new Insets(0, 0, 5, 5);
		gbc_lblTarget.gridx = 0;
		gbc_lblTarget.gridy = 5;
		add(lblTarget, gbc_lblTarget);
		
		powerTarget = new JTextField();
		powerTarget.setBackground(new Color(255, 222, 173));
		powerTarget.setText("One Creature");
		GridBagConstraints gbc_txtOneCreature = new GridBagConstraints();
		gbc_txtOneCreature.fill = GridBagConstraints.BOTH;
		gbc_txtOneCreature.insets = new Insets(0, 0, 5, 0);
		gbc_txtOneCreature.gridx = 1;
		gbc_txtOneCreature.gridy = 5;
		add(powerTarget, gbc_txtOneCreature);
		powerTarget.setColumns(10);
		
		lblAttack = new JLabel("Attack");
		lblAttack.setHorizontalAlignment(SwingConstants.RIGHT);
		GridBagConstraints gbc_lblAttack = new GridBagConstraints();
		gbc_lblAttack.fill = GridBagConstraints.BOTH;
		gbc_lblAttack.insets = new Insets(0, 0, 5, 5);
		gbc_lblAttack.gridx = 0;
		gbc_lblAttack.gridy = 6;
		add(lblAttack, gbc_lblAttack);
		
		powerAttack = new JTextField();
		powerAttack.setBackground(new Color(255, 222, 173));
		powerAttack.setText("Str vs AC");
		GridBagConstraints gbc_txtStrVsAc = new GridBagConstraints();
		gbc_txtStrVsAc.fill = GridBagConstraints.BOTH;
		gbc_txtStrVsAc.insets = new Insets(0, 0, 5, 0);
		gbc_txtStrVsAc.gridx = 1;
		gbc_txtStrVsAc.gridy = 6;
		add(powerAttack, gbc_txtStrVsAc);
		powerAttack.setColumns(10);
		
		lblHit = new JLabel("Hit");
		lblHit.setHorizontalAlignment(SwingConstants.RIGHT);
		GridBagConstraints gbc_lblHit = new GridBagConstraints();
		gbc_lblHit.fill = GridBagConstraints.BOTH;
		gbc_lblHit.insets = new Insets(0, 0, 5, 5);
		gbc_lblHit.gridx = 0;
		gbc_lblHit.gridy = 7;
		add(lblHit, gbc_lblHit);
		
		powerHit = new JTextField();
		powerHit.setBackground(new Color(255, 222, 173));
		powerHit.setText("1[W]");
		GridBagConstraints gbc_txtw = new GridBagConstraints();
		gbc_txtw.fill = GridBagConstraints.BOTH;
		gbc_txtw.insets = new Insets(0, 0, 5, 0);
		gbc_txtw.gridx = 1;
		gbc_txtw.gridy = 7;
		add(powerHit, gbc_txtw);
		powerHit.setColumns(10);
		
		lblEffect = new JLabel("Effect");
		lblEffect.setHorizontalAlignment(SwingConstants.RIGHT);
		GridBagConstraints gbc_lblEffect = new GridBagConstraints();
		gbc_lblEffect.fill = GridBagConstraints.BOTH;
		gbc_lblEffect.insets = new Insets(0, 0, 5, 5);
		gbc_lblEffect.gridx = 0;
		gbc_lblEffect.gridy = 8;
		add(lblEffect, gbc_lblEffect);
		
		powerEffect = new JTextArea();
		powerEffect.setForeground(new Color(0, 0, 0));
		powerEffect.setBackground(new Color(255, 222, 173));
		powerEffect.setRows(6);
		powerEffect.setColumns(24);
		powerEffect.setLineWrap(true);
		powerEffect.setWrapStyleWord(true);
		powerEffect.setText("The target is stunned.");
		GridBagConstraints gbc_txtrTheTargetIs = new GridBagConstraints();
		gbc_txtrTheTargetIs.insets = new Insets(0, 0, 5, 0);
		gbc_txtrTheTargetIs.fill = GridBagConstraints.BOTH;
		gbc_txtrTheTargetIs.gridx = 1;
		gbc_txtrTheTargetIs.gridy = 8;
		add(powerEffect, gbc_txtrTheTargetIs);
		
		chckbxNoEffect = new JCheckBox("No Effect");
		chckbxNoEffect.setBackground(new Color(255, 222, 173));
		chckbxNoEffect.setHorizontalAlignment(SwingConstants.LEFT);
		chckbxNoEffect.setToolTipText("Select if spell has no extra effects.");
		GridBagConstraints gbc_chckbxNoEffect = new GridBagConstraints();
		gbc_chckbxNoEffect.insets = new Insets(0, 0, 5, 0);
		gbc_chckbxNoEffect.fill = GridBagConstraints.BOTH;
		gbc_chckbxNoEffect.gridx = 1;
		gbc_chckbxNoEffect.gridy = 9;
		add(chckbxNoEffect, gbc_chckbxNoEffect);
		
		btnGenerate = new JButton("Generate!");
		GridBagConstraints gbc_btnGenerate = new GridBagConstraints();
		gbc_btnGenerate.insets = new Insets(0, 0, 5, 0);
		gbc_btnGenerate.gridx = 1;
		gbc_btnGenerate.gridy = 10;
		add(btnGenerate, gbc_btnGenerate);

		// Save button!
		btnSave = new JButton("Save!");
		GridBagConstraints gbc_btnSave = new GridBagConstraints();
		gbc_btnSave.insets = new Insets(0, 0, 5, 0);
		gbc_btnSave.gridx = 1;
		gbc_btnSave.gridy = 11;
		add(btnSave,gbc_btnSave);
		
		// Shows progress or errors.
		lblWaiting = new JLabel("Ready");
		GridBagConstraints gbc_lblWaiting = new GridBagConstraints();
		gbc_lblWaiting.gridx = 1;
		gbc_lblWaiting.gridy = 12;
		add(lblWaiting, gbc_lblWaiting);
		
		
		/*
		 *  End creating components. Begin adding actionlisteners.
		 */
		
		
		// Add a listener to the "Generate" button. This creates a new cardview that 
		// displays the generated image.
		btnGenerate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				boolean check = chckbxNoEffect.isSelected();
				Generator gen = new Generator();
				gen.setName(powerName.getText());
				gen.setType(powerType.getText());
				gen.setAttributes(powerKeywords.getText());
				gen.setActionType(powerActionType.getText());
				gen.setRange(powerRange.getText());
				gen.setTarget(powerTarget.getText());
				gen.setAttack(powerAttack.getText());
				gen.setHit(powerHit.getText());
				gen.setEffect(powerEffect.getText());
				gen.setNoEffect(check);
				
				setCardView(new ImageIcon(gen.generateImg()));
			}
		});
		
		// Add an action listener to the save button. This brings up
		// a save dialog and functions as you would expect.
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				boolean check = chckbxNoEffect.isSelected();
				Generator gen = new Generator();
				gen.setName(powerName.getText());
				gen.setType(powerType.getText());
				gen.setAttributes(powerKeywords.getText());
				gen.setActionType(powerActionType.getText());
				gen.setRange(powerRange.getText());
				gen.setTarget(powerTarget.getText());
				gen.setAttack(powerAttack.getText());
				gen.setHit(powerHit.getText());
				gen.setEffect(powerEffect.getText());
				gen.setNoEffect(check);
				
				gen.saveFile();
			}
		});


	}
	
	/**
	 * Create a new CardView with new ImageIcon i.
	 * CardView resets each time, so only one image
	 * is displayed.
	 * @param i
	 */
	public void setCardView(ImageIcon i) {
		card.CreateView(i);
	}

}
